package io.renren.modules.store.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.common.utils.BeanJudgeUtil;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStorePayMethodEntity;
import io.renren.modules.store.service.TStorePayMethodService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-25 21:22:21
 */
@RestController
@RequestMapping("store/tstorepaymethod")
public class TStorePayMethodController extends AbstractController {
    @Autowired
    private TStorePayMethodService tStorePayMethodService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstorepaymethod:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        params.put("key",params.get("key"));
        params.put("status",params.get("status"));
        params.put("sysBuiltIn",params.get("sysBuiltIn"));
        PageUtils page = tStorePayMethodService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{payMethodId}")
    @RequiresPermissions("store:tstorepaymethod:info")
    public R info(@PathVariable("payMethodId") Long payMethodId){
		TStorePayMethodEntity tStorePayMethod = tStorePayMethodService.getById(payMethodId);

        return R.ok().put("tStorePayMethod", tStorePayMethod);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("store:tstorepaymethod:save")
    @NoRepeatSubmit
    public R save(@RequestBody TStorePayMethodEntity tStorePayMethod){
        TStorePayMethodEntity one = tStorePayMethodService.getOne(new QueryWrapper<TStorePayMethodEntity>()
                .eq("seller_id", getSeller())
                .eq("pay_method_name", tStorePayMethod.getPayMethodName())
        );
        if (!BeanJudgeUtil.beanIsNull(one)){
            throw new RRException("支付方式已存在");
        }
        TStorePayMethodEntity one1 = tStorePayMethodService.getOne(new QueryWrapper<TStorePayMethodEntity>()
                .eq("sys_built_in", 1)
                .eq("pay_method_name", tStorePayMethod.getPayMethodName())
        );
        if (!BeanJudgeUtil.beanIsNull(one1)){
            throw new RRException("系统已内置该支付方式,不能添加");
        }
        tStorePayMethod.setSellerId(getSeller());

		tStorePayMethodService.save(tStorePayMethod);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstorepaymethod:update")
    public R update(@RequestBody TStorePayMethodEntity tStorePayMethod){
		tStorePayMethodService.updateById(tStorePayMethod);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstorepaymethod:delete")
    public R delete(@RequestBody Long[] payMethodIds){
		tStorePayMethodService.removeByIds(Arrays.asList(payMethodIds));

        return R.ok();
    }

}
